<template>
  <div class="modal-overlay" v-if="visible" @click="closeModal">
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h3>修改生产日期和数量</h3>
        <button @click="closeModal" class="close-btn">✕</button>
      </div>
      
      <div class="modal-body">
        <!-- 商品信息 -->
        <div class="product-info">
          <h4>{{ product?.productName }}</h4>
          <div class="product-details">
            <div class="detail-item">
              <span class="detail-label">供应商:</span>
              <span class="detail-value">{{ product?.supplier }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">条码:</span>
              <span class="detail-value">{{ product?.barcode }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">库位:</span>
              <span class="detail-value">{{ product?.locationCode }}</span>
            </div>
          </div>
        </div>

        <!-- 日期表单 -->
        <div class="date-form">
          <div class="date-field">
            <label>当前效期</label>
            <input 
              type="text" 
              :value="formatDate(product?.productionDate)" 
              readonly 
              class="date-input readonly"
            />
          </div>
          
          <div class="date-field">
            <label>效期调整</label>
            <input 
              type="date" 
              v-model="newDate" 
              class="date-input"
              :max="maxDate"
            />
          </div>

          <!-- 商品数量修改 -->
          <div class="quantity-field">
            <label>可用库存</label>
            <input 
              type="text" 
              :value="product?.availableStock || product?.quantity || 0" 
              readonly 
              class="quantity-input readonly"
            />
          </div>
          
          <div class="quantity-field">
            <label>修改商品数</label>
            <input 
              type="number" 
              v-model.number="newQuantity" 
              class="quantity-input"
              min="0"
              step="1"
            />
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button @click="closeModal" class="cancel-btn">取消</button>
        <button 
          @click="submitChange" 
          :disabled="(!newDate && newQuantity === null) || submitting"
          class="submit-btn"
        >
          {{ submitting ? '提交中...' : '确定' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, defineProps, defineEmits } from 'vue';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  product: {
    type: Object,
    default: () => ({})
  }
});

const emit = defineEmits(['close', 'submit']);

const newDate = ref('');
const newQuantity = ref(null);
const submitting = ref(false);

// 最大日期（今天）
const maxDate = computed(() => {
  return new Date().toISOString().split('T')[0];
});

// 监听弹窗显示状态，重置表单
watch(() => props.visible, (visible) => {
  if (visible) {
    newDate.value = '';
    newQuantity.value = props.product?.availableStock || props.product?.quantity || null;
    submitting.value = false;
  }
});

// 格式化日期显示
function formatDate(dateString) {
  if (!dateString) return '-';
  
  try {
    const date = new Date(dateString);
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    });
  } catch (error) {
    return dateString;
  }
}

// 关闭弹窗
function closeModal() {
  emit('close');
}

// 提交修改
async function submitChange() {
  if ((!newDate.value && newQuantity.value === null) || submitting.value) return;
  
  submitting.value = true;
  
  try {
    // 准备webhook数据
    const webhookData = {
      recordId: props.product.id || props.product.recordId, // 库位库存记录ID
      oldProductionDate: props.product.productionDate, // 修改前的生产日期
      newProductionDate: newDate.value, // 修改后的生产日期
      availableStock: props.product.availableStock || props.product.quantity, // 可用库存
      modifiedQuantity: newQuantity.value, // 修改商品数
      productName: props.product.productName,
      barcode: props.product.barcode,
      locationCode: props.product.locationCode,
      supplier: props.product.supplier,
      timestamp: new Date().toISOString()
    };
    
    emit('submit', webhookData);
  } finally {
    submitting.value = false;
  }
}
</script>

<style scoped>
/* 弹窗覆盖层 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

/* 弹窗容器 */
.modal-container {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 480px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  animation: modalShow 0.3s ease-out;
}

@keyframes modalShow {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  background: #f8f9fa;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  color: #000000;
}

.close-btn {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #666666;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #e9ecef;
  color: #000000;
}

/* 弹窗主体 */
.modal-body {
  padding: 20px;
}

/* 商品信息 */
.product-info {
  margin-bottom: 24px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.06);
}

.product-info h4 {
  margin: 0 0 12px 0;
  font-size: 16px;
  font-weight: 700;
  color: #000000;
}

.product-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.detail-label {
  font-size: 14px;
  color: #666666;
  font-weight: 500;
  min-width: 60px;
}

.detail-value {
  font-size: 14px;
  color: #000000;
  font-weight: 600;
  flex: 1;
}

/* 日期表单 */
.date-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.date-field,
.quantity-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.date-field label,
.quantity-field label {
  font-size: 14px;
  font-weight: 600;
  color: #000000;
}

.date-input,
.quantity-input {
  padding: 12px 14px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 8px;
  font-size: 14px;
  color: #000000;
  background: #fff;
  transition: border-color 0.3s ease;
}

.date-input:focus,
.quantity-input:focus {
  outline: none;
  border-color: #f8b300;
  box-shadow: 0 2px 8px rgba(248, 179, 0, 0.1);
}

.date-input.readonly,
.quantity-input.readonly {
  background: #f8f9fa;
  color: #666666;
  cursor: not-allowed;
}

/* 弹窗底部 */
.modal-footer {
  display: flex;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  background: #f8f9fa;
}

.cancel-btn,
.submit-btn {
  flex: 1;
  height: 44px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.cancel-btn {
  background: #e9ecef;
  color: #495057;
}

.cancel-btn:hover {
  background: #dee2e6;
}

.submit-btn {
  background: #f8b300;
  color: #000000;
}

.submit-btn:hover:not(:disabled) {
  background: #e0a200;
}

.submit-btn:disabled {
  background: #cccccc;
  color: #999999;
  cursor: not-allowed;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .modal-overlay {
    padding: 8px;
    align-items: flex-start;
    padding-top: 20px;
  }
  
  .modal-container {
    max-width: 100%;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
  }
  
  .modal-header {
    padding: 12px 16px;
    position: sticky;
    top: 0;
    z-index: 10;
  }
  
  .modal-header h3 {
    font-size: 16px;
  }
  
  .modal-body {
    padding: 16px;
    max-height: none;
  }
  
  .product-info {
    margin-bottom: 16px;
    padding: 12px;
  }
  
  .product-info h4 {
    font-size: 15px;
    margin-bottom: 8px;
  }
  
  .product-details {
    display: grid;
    gap: 6px;
  }
  
  .detail-item {
    display: grid;
    grid-template-columns: 70px 1fr;
    gap: 8px;
    align-items: center;
  }
  
  .detail-label {
    font-size: 12px;
    font-weight: 600;
  }
  
  .detail-value {
    font-size: 12px;
    word-break: break-all;
  }
  
  .date-form {
    gap: 12px;
  }
  
  .date-field,
  .quantity-field {
    gap: 6px;
  }
  
  .date-field label,
  .quantity-field label {
    font-size: 13px;
    font-weight: 700;
  }
  
  .date-input,
  .quantity-input {
    padding: 14px 16px;
    font-size: 16px; /* 防止iOS缩放 */
    border-radius: 10px;
    border: 2px solid rgba(0, 0, 0, 0.1);
  }
  
  .date-input:focus,
  .quantity-input:focus {
    border-color: #f8b300;
    box-shadow: 0 0 0 3px rgba(248, 179, 0, 0.1);
  }
  
  .modal-footer {
    padding: 12px 16px;
    gap: 8px;
    position: sticky;
    bottom: 0;
    background: #f8f9fa;
    border-top: 2px solid rgba(0, 0, 0, 0.06);
  }
  
  .cancel-btn,
  .submit-btn {
    height: 48px;
    font-size: 15px;
    font-weight: 700;
    border-radius: 10px;
  }
}

@media (max-width: 480px) {
  .modal-overlay {
    padding: 4px;
    padding-top: 10px;
  }
  
  .modal-container {
    max-height: calc(100vh - 20px);
    border-radius: 20px 20px 0 0;
  }
  
  .modal-header {
    padding: 16px;
    border-radius: 20px 20px 0 0;
  }
  
  .modal-header h3 {
    font-size: 18px;
    font-weight: 800;
  }
  
  .close-btn {
    width: 36px;
    height: 36px;
    font-size: 20px;
  }
  
  .modal-body {
    padding: 16px;
  }
  
  .product-info {
    margin-bottom: 16px;
    padding: 16px;
    border-radius: 16px;
  }
  
  .product-info h4 {
    font-size: 16px;
    font-weight: 800;
    margin-bottom: 12px;
  }
  
  .detail-item {
    grid-template-columns: 80px 1fr;
    gap: 12px;
    padding: 4px 0;
  }
  
  .detail-label {
    font-size: 13px;
  }
  
  .detail-value {
    font-size: 13px;
    font-weight: 700;
  }
  
  .date-field,
  .quantity-field {
    gap: 8px;
  }
  
  .date-field label,
  .quantity-field label {
    font-size: 14px;
    font-weight: 800;
  }
  
  .date-input,
  .quantity-input {
    padding: 16px;
    font-size: 16px;
    border-radius: 12px;
    min-height: 24px;
  }
  
  .modal-footer {
    padding: 16px;
    gap: 12px;
  }
  
  .cancel-btn,
  .submit-btn {
    height: 52px;
    font-size: 16px;
    font-weight: 800;
    border-radius: 12px;
  }
}

/* 超小屏幕优化 */
@media (max-width: 360px) {
  .modal-overlay {
    padding: 0;
  }
  
  .modal-container {
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
  }
  
  .modal-header {
    border-radius: 0;
  }
  
  .detail-item {
    grid-template-columns: 65px 1fr;
    gap: 8px;
  }
  
  .detail-label {
    font-size: 12px;
  }
  
  .detail-value {
    font-size: 12px;
  }
}
</style>